package pacman;

import java.io.IOException;

import javax.microedition.midlet.*; 
import javax.microedition.lcdui.*; 
import javax.microedition.lcdui.game.Sprite;

public class PacmanTest extends MIDlet implements CommandListener { 

	private Command goToPauseMenu, okCommandMainMenu,okCommandPauseMenu,goToMainMenu; 
	private Display display; 
	private SSCanvas PacmanGameScreen; 
	private Form MenuScreen;
	private List mainMenuList,pauseList;
	private Thread gameThread;
	private boolean isActiveGame, isFromSettings,isCreatedGame;
	private Gauge levelGauge;
	private int level = 1;

	public PacmanTest() { 
		this.level = 1;
		display=Display.getDisplay(this); 
		isActiveGame=false;
		isFromSettings=false;

	} 

	public void displayPauseMenu()
	{
		String[] opcionesPlayMenuString = {"Continue","End game"};
		try
		{
			Image[] image = new Image[2];
			image[0]=Image.createImage("/PurpleEnemy.png");
			image[1]=Image.createImage("/OrangeEnemy.png");
			pauseList = new List("Main Menu",List.IMPLICIT,opcionesPlayMenuString,image);
		}
		catch(Exception e)
		{
			System.out.println("no encontro la imagen en displayPauseMenu");
		}
		okCommandPauseMenu = new Command("Ok",Command.OK,2);
		pauseList.addCommand(okCommandPauseMenu);
		pauseList.setCommandListener(this);

		display.setCurrent(pauseList);
	}

	public void displayMainMenu()
	{
		okCommandMainMenu = new Command("OK",Command.OK,2);
		String[] opcionesMainMenuString = {"Start new game","Settings","High scores","Exit"};
		
		try
		{
			Image[] image = new Image[4];
			image[0]=Image.createImage("/PacmanJunto.png");
			image[1]=Image.createImage("/BlueEnemy.png");
			image[2]=Image.createImage("/GreenEnemy.png");
			image[3]=Image.createImage("/RedEnemy.png");
			mainMenuList = new List("Main Menu",List.IMPLICIT,opcionesMainMenuString,image);
		}
		catch(Exception e)
		{
			System.out.println("no encontro la imagen en displayMainMenu");
		}

		mainMenuList.addCommand(okCommandMainMenu);
		mainMenuList.setCommandListener(this);
		display.setCurrent(mainMenuList);
	}

	public void displayStartGame()
	{
		goToPauseMenu = new Command("Pause",Command.SCREEN,2);
		//goToMenuCommand = new Command("GoToMenu",Command.SCREEN,2);
		PacmanGameScreen=new SSCanvas(); 
		PacmanGameScreen.setLevel(level);
		PacmanGameScreen.addCommand(goToPauseMenu);
		PacmanGameScreen.setCommandListener(this); 
		PacmanGameScreen.start();
		isActiveGame=true;
		isCreatedGame=true;
		display.setCurrent(PacmanGameScreen);
	}

	public void displayContinueGame()
	{
		isActiveGame=true;
		display.setCurrent(PacmanGameScreen);
		PacmanGameScreen.Continue();
		
	}

	public void displaySettigs()
	{
		goToMainMenu = new Command("Save",Command.SCREEN,2);
		System.out.println("entro bien a displaySettings()");
		isFromSettings = true;
		MenuScreen = new Form("Settings:");
		levelGauge = new Gauge("Level: ",true,5,1);
		MenuScreen.append(levelGauge);
		MenuScreen.addCommand(goToMainMenu);
		MenuScreen.setCommandListener(this);
		display.setCurrent(MenuScreen);
	}

	public void startApp() throws MIDletStateChangeException { 
		displayMainMenu();
	} 

	public void pauseApp() {} 

	public void destroyApp(boolean unconditional) {
		
		
	} 

	public void commandAction(Command c, Displayable s) { 
		if (c == goToPauseMenu) { 
			try{
				PacmanGameScreen.stop();
			}
			catch(Exception e){
				System.out.println(e.toString());
			}
			this.displayPauseMenu();
		}

		else if(c==okCommandMainMenu)
		{
			int option = mainMenuList.getSelectedIndex();

			switch(option)
			{
			case 0: //case New Game
				displayStartGame();
				break;
			case 1: //case Settings
				displaySettigs();
				break;
			case 2: //case High scores
				System.out.println(option);
				break;
			case 3: //case Exit
				destroyApp(false);
				notifyDestroyed();
				break;
			}
		}
		else if(c==okCommandPauseMenu)
		{
			int option = pauseList.getSelectedIndex();

			switch(option)
			{
			case 0: //case Continue
				displayContinueGame();
				break;
			case 1: //case End game
				displayMainMenu();
				break;
			}
		}
		else if(c==goToMainMenu)
		{
			this.level = this.levelGauge.getValue();
			displayMainMenu();
		}
	}
}